﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;

namespace Ciripescu_WebRole
{
    public partial class vote : System.Web.UI.Page
    {
        public string userName = "";
        public string nrVoturi = "";
        public string sePoateVotaIn = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["userName"] != null)
            {
                userName = Request.QueryString["userName"];
                UserManager um = new UserManager();
                var user = um.GetItem(userName);
                if (user == null)
                {
                    errors.InnerText = "Link-ul de vot folosit este invalid!";
                    return;
                }
                if (!IsPostBack)
                {
                    Random r = new Random();
                    string cod = r.Next().ToString() + r.Next().ToString();
                    ViewState.Add("cod", cod);
                    Session.Add("cod", cod);
                    divJavascript.Visible = true;
                }
            }
            else
            {
                errors.InnerText = "Link-ul de vot folosit este invalid!";
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            //Timer1.Enabled = false;
            ProcessVote();
        }

        private void ProcessVote()
        {
            divJavascript.Visible = false;
            if (Session["cod"] != null && (string)Session["cod"] == (string)ViewState["cod"])
            {                
                VotManager vm = new VotManager();
                string ip = Request.UserHostAddress;
                var vot = vm.PoateVota(userName, ip);
                if (vot != null)
                {
                    TimeSpan ts = vot.time.AddHours(24).Subtract(DateTime.Now);
                    sePoateVotaIn = ts.Hours + " ore, " + ts.Minutes + " minute si " + ts.Seconds + " secunde";
                    vot_prea_des.Visible = true;
                    return;
                }
                else
                {
                    vot_ok.Visible = true;
                    Vot v = new Vot(userName, DateTime.Now, ip, Request.UserAgent);
                    vm.Insert(v);
                    UserManager um = new UserManager();
                    var user = um.GetItem(userName);
                    user.Votes = vm.CalcNbOfVotes(userName);
                    um.Save(user);
                    nrVoturi = user.Votes.ToString();
                }
            }
            else
            {
                errors.InnerText = "Posibila incercare de fraudare a votului! Votul nu a fost contorizat, dar a fost logat :P";
            }
        }

        protected void VoteB_Click(object sender, EventArgs e)
        {
            ProcessVote();
        }
    }
}
